package com.gxitsky.exception;

/**
 * @author gxing
 * @desc 自定义系统级错误编码
 * @date 2021/9/20 22:10
 */
public enum SystemError implements Error {

    SYS_INTERNAL_ERROR(500, "系统异常"),

    /*--------------Request--------------*/
    REQUEST_URI_NOT_FOUND(404, "请求路径不存在"),
    REQUEST_METHOD_NOT_SUPPORT(405, "请求方式不支持"),
    REQUEST_BODY_NOT_READABLE(406, "请求体格式错误不可读"),
    REQUEST_CONTENT_TYPE_NOT_SUPPORT(407, "请求Content-Type不支持"),
    REQUEST_PARAM_VALID_ERROR(408, "请求参数校验错误"),
    REQUEST_PARAM_TYPE_ERROR(409, "请求参数类型错误"),
    REQUEST_BODY_CONVERT_ERROR(410, "请求体转换错误"),
    REQUEST_BODY_LENGTH_OUT_RANGE(411, "请求体长度越界"),

    /*---------------Method--------------------*/
    METHOD_ARG_EXCEPTION(430,"向方法传递了非法或不适当的参数"),
    METHOD_ARG_TYPE_MISMATCH_EXCEPTION(431, "方法参数类型不匹配"),
    METHOD_ARG_NUMBER_FORMAT_EXCEPTION(432, "字符串转数字格式错误"),



    /*--------------数据库操作------------*/
    DB_INSERT_FAIL(400101, "新增失败"),
    DB_UPDATE_FAIL(400102, "更新失败"),
    DB_DELETE_FAIL(400103, "删除失败"),
    DB_DUPLICATE_KEY_FAIL(400104, "字段已存在"),
    DB_UNIQUE_INDEX_CONFLICT(400105, "唯一索引冲突"),
    DB_DATA_INTEGRITY_VIOLATION_EXCEPTION(400106, "数据违反完整性约束"),

    /*--------------文件上传-------------*/
    FILE_UPLOAD_FAIL(400201, "文件失败"),
    FILE_UPLOAD_SIZE_OUT_MAX(400202, "文件大小超出限制"),

    /*---------------导入导出-------------------*/
    IMPORT_FAIL(400301, "导入失败"),
    IMPORT_DATA_EMPTY(400302, "导入数据不能为空"),
    EXPORT_FAIL(400303, "导出数据失败"),
    EXPORT_DATA_FAIL(400304, "导出数据失败"),

    ;

    private int code;
    private String message;

    SystemError(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Error parse(int code) {
        SystemError[] values = SystemError.values();
        for (int i = 0; i < values.length; i++) {
            SystemError value = values[i];
            if (value.code == code) {
                return value;
            }
        }
        return null;
    }
}
